using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DailyTestBLL
{
    public class DataAccess
    {

        private string m_ConnStr = string.Empty;
        public string ConnStr
        {
            get 
            {
                return m_ConnStr;
            }
            set 
            {
                m_ConnStr = value;
            }
        }

        SqlConnection conn = null;

        private bool Open()
        {
            if (conn == null)
            {
                conn = new SqlConnection();
            }
            if (conn.State == ConnectionState.Closed )
            {
                conn.ConnectionString = m_ConnStr;
            }
            if ( conn.State == ConnectionState.Closed )
            {
                conn.Open();
            }
            return true;
        }

        private bool Close()
        {
            if( conn != null && conn.State != ConnectionState.Closed )
            {
                conn.Close();
            }
            if (conn != null)
            {
                conn.Dispose();
                conn = null;
            }
            return true;
        }

        

        public DataTable GetData( RunWay way,params string[] param )
        {
            DataTable res = new DataTable();
            switch (way)
            {
                case RunWay.Sql:
                    try
                    {
                        Open();
                        SqlCommand cmd = conn.CreateCommand();
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = param[0];
                        SqlDataAdapter adp = new SqlDataAdapter();
                        adp.SelectCommand = cmd;
                        adp.Fill(res);
                        Close();
                    }
                    catch(System.Exception ex)
                    {
                        string str = ex.Message;
                        Close();
                    }
                    break;
                case RunWay.Procedure:
                    try
                    {
                        Open();
                        SqlCommand cmd = conn.CreateCommand();
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = param[0];
                        cmd.Parameters.AddWithValue("inputXML", param[1]);
                        SqlDataAdapter adp = new SqlDataAdapter();
                        adp.SelectCommand = cmd;
                        adp.Fill(res);
                        Close();
                    }
                    catch (System.Exception ex)
                    {
                        string s = ex.Message;
                        Close();
                    }
                    break;
                default:
                    break;
            }
            return res;
        }
    }

    public enum RunWay { Sql = 1,Procedure =2,Other =3 }
}
